package com.lee.study;

import dev.langchain4j.data.document.Document;

import java.nio.file.Paths;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        String ollamaUrl = "http://localhost:11434";
        String esUrl = "http://localhost:9200";
        String index = "es_knowledge";
        String model = "bge-m3";

        /* 1. 解析知识库 */
        List<Document> docs = MarkdownLoader.loadDir(
                Paths.get("../data"));

        /* 2. 向量化 + 入库 */
        EmbeddingService embSvc = new EmbeddingService(ollamaUrl, model);
        ElasticsearchStore store = new ElasticsearchStore(esUrl, index);
        store.ingest(docs, embSvc);

        /* 3. 交互问答 */
        try (java.io.BufferedReader br = new java.io.BufferedReader(
                new java.io.InputStreamReader(System.in))) {
            while (true) {
                System.out.print("问：");
                String q = br.readLine();
                if ("q".equalsIgnoreCase(q)) {
                    System.out.println("exiting...");
                    break;
                }
                var list = store.search(q, embSvc, 3);
                list.forEach(m -> System.out.printf("【%.3f】%s%n",
                        m.score(), m.embedded().text()));
            }
        }

        store.fini();

    }
}